﻿using System;
using System.Collections.Generic;

using System.Text;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace Xeno.Core
{
    public class XenoRenderTarget
    {
        public int Buffer {get;private set;}
        public XenoRenderTarget()
        {
            int buf = 0;
            GL.GenFramebuffers(1, out buf);
            Buffer = buf;
        }
        public void Bind()
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, Buffer);
        }
        public void Unbind()
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
        public void AttachTexture(XenoTexture texture)
        {
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer,FramebufferAttachment.ColorAttachment0,TextureTarget.Texture2D, texture.Texture, 0);
            CheckStatus();
            GL.Clear(ClearBufferMask.ColorBufferBit);
        }
        public void AttachDepth(XenoTexture texture)
        {
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureTarget.Texture2D, texture.Texture, 0);
            CheckStatus();
            GL.Clear(ClearBufferMask.DepthBufferBit);

        }
        public void CheckStatus()
        {
            FramebufferErrorCode ec = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
            if (ec != FramebufferErrorCode.FramebufferComplete)
            {
                XenoDebug.LogError("FrameBuffer:  "+ec.ToString(), "XenoRenderTarget.CheckStatus");
            }
        }
    }
}
